package org.example.service.Impl;

import org.example.commonapi.domain.R;
import org.example.commonapi.domain.onlineCode.CommitRecord;
import org.example.commonapi.domain.onlineCode.CommitStatistics;
import org.example.commonapi.domain.onlineCode.RankForOj;
import org.example.commonapi.dto.onlineCode.CommitStatisticsDTO;
import org.example.commonapi.mongo.CommitRecordRepository;
import org.example.commonapi.mongo.CommitStatisticsRepository;
import org.example.commonapi.mongo.RankForOjRepository;
import org.example.service.RankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RankServiceImpl implements RankService {
    @Autowired
    private CommitRecordRepository commitRecordRepository;

    @Autowired
    private CommitStatisticsRepository commitStatisticsRepository;

    @Autowired
    private RankForOjRepository rankForOjRepository;

    @Override
    public R<Object> getOjRankTime(Integer page, Integer size) {
        return getOjRank("time", page, size);
    }

    @Override
    public R<Object> getOjRankMemory(Integer page, Integer size) {
        return getOjRank("memory", page, size);
    }

    @Override
    public R<Object> echarts(String problemId) {
        List<CommitRecord> byProblemId = commitRecordRepository.findByProblemId(problemId);
        if (byProblemId.isEmpty()) {
            return new R<>(null,404, "没有数据");
        }

        return new R<>(byProblemId,200, "获取成功");
    }
    @Override
    public R<Object> echarts20(String problemId) {
        List<CommitRecord> byProblemId = commitRecordRepository.findByProblemId(problemId);
        if (byProblemId.isEmpty()) {
            return new R<>(null,404, "没有数据");
        }
        if (byProblemId.size() > 20) {
            byProblemId = byProblemId.subList(byProblemId.size()-21, byProblemId.size()-1);
        }
        byProblemId.sort((o1, o2) -> o2.getSubmitTime().compareTo(o1.getSubmitTime()));
        return new R<>(byProblemId,200, "获取成功");
    }

    @Override
    public R<Object> statistics(String problemId) {
        List<CommitStatistics> byProblemId = commitStatisticsRepository.findByProblemId(problemId);
        if (byProblemId.isEmpty()) {
            return new R<>(null,404, "没有数据");
        }
        int sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;
        List<CommitStatisticsDTO> list = CommitStatisticsDTO.toList(byProblemId);
        for (CommitStatistics commitStatistics : byProblemId) {
            if (commitStatistics.getType() == 0) {
                sum1 += commitStatistics.getValue();
            } else if (commitStatistics.getType() == 1) {
                sum2 += commitStatistics.getValue();
            } else {
                sum3 += commitStatistics.getValue();
            }
        }
        for (CommitStatisticsDTO commitStatisticsDTO : list) {
            if (commitStatisticsDTO.getType() == 0) {
                commitStatisticsDTO.setValue((double) commitStatisticsDTO.getValue() / sum1);
            }
            if (commitStatisticsDTO.getType() == 1) {
                commitStatisticsDTO.setValue((double) commitStatisticsDTO.getValue() / sum2);
            }
            if (commitStatisticsDTO.getType() == 2) {
                commitStatisticsDTO.setValue((double) commitStatisticsDTO.getValue() / sum3);
            }
        }
        return new R<>(list,200, "获取成功");
    }

    public R<Object> getOjRank(String sortField, Integer page, Integer size) {
        Sort sort = Sort.by(Sort.Direction.ASC, sortField);
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<RankForOj> all = rankForOjRepository.findAll(pageable);
        if (all.isEmpty()) {
            return new R<>(null,404, "没有数据");
        }
        return new R<>(all.get(),200, "获取成功");
    }
}
